home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / tk2.3 / dist / tkSelect.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-10  |  53.2 KB  |  1,865 lines

  1. /* 
  2.  * tkSelect.c --
  3.  *
  4.  *    This file manages the selection for the Tk toolkit,
  5.  *    translating between the standard X ICCCM conventions
  6.  *    and Tcl commands.
  7.  *
  8.  * Copyright 1990 Regents of the University of California.
  9.  * Permission to use, copy, modify, and distribute this
  10.  * software and its documentation for any purpose and without
  11.  * fee is hereby granted, provided that the above copyright
  12.  * notice appear in all copies.  The University of California
  13.  * makes no representations about the suitability of this
  14.  * software for any purpose.  It is provided "as is" without
  15.  * express or implied warranty.
  16.  */
  17.  
  18. #ifndef lint
  19. static char rcsid[] = "$Header: /user6/ouster/wish/RCS/tkSelect.c,v 1.27 92/08/10 15:03:03 ouster Exp $ SPRITE (Berkeley)";
  20. #endif
  21.  
  22. #include "tkConfig.h"
  23. #include "tkInt.h"
  24.  
  25. /*
  26.  * When the selection is being retrieved, one of the following
  27.  * structures is present on a list of pending selection retrievals.
  28.  * The structure is used to communicate between the background
  29.  * procedure that requests the selection and the foreground
  30.  * event handler that processes the events in which the selection
  31.  * is returned.  There is a list of such structures so that there
  32.  * can be multiple simultaneous selection retrievals (e.g. on
  33.  * different displays).
  34.  */
  35.  
  36. typedef struct RetrievalInfo {
  37.     Tcl_Interp *interp;        /* Interpreter for error reporting. */
  38.     TkWindow *winPtr;        /* Window used as requestor for
  39.                  * selection. */
  40.     Atom property;        /* Property where selection will appear. */
  41.     Atom target;        /* Desired form for selection. */
  42.     int (*proc) _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp,
  43.     char *portion));    /* Procedure to call to handle pieces
  44.                  * of selection. */
  45.     ClientData clientData;    /* Argument for proc. */
  46.     int result;            /* Initially -1.  Set to a Tcl
  47.                  * return value once the selection
  48.                  * has been retrieved. */
  49.     Tk_TimerToken timeout;    /* Token for current timeout procedure. */
  50.     int idleTime;        /* Number of seconds that have gone by
  51.                  * without hearing anything from the
  52.                  * selection owner. */
  53.     struct RetrievalInfo *nextPtr;
  54.                 /* Next in list of all pending
  55.                  * selection retrievals.  NULL means
  56.                  * end of list. */
  57. } RetrievalInfo;
  58.  
  59. static RetrievalInfo *pendingRetrievals = NULL;
  60.                 /* List of all retrievals currently
  61.                  * being waited for. */
  62.  
  63. /*
  64.  * When "selection get" is being used to retrieve the selection,
  65.  * the following data structure is used for communication between
  66.  * Tk_SelectionCmd and SelGetProc.  Its purpose is to keep track
  67.  * of the selection contents, which are gradually assembled in a
  68.  * string.
  69.  */
  70.  
  71. typedef struct {
  72.     char *string;        /* Contents of selection are
  73.                  * here.  This space is malloc-ed. */
  74.     int bytesAvl;        /* Total number of bytes available
  75.                  * at string. */
  76.     int bytesUsed;        /* Bytes currently in use in string,
  77.                  * not including the terminating
  78.                  * NULL. */
  79. } GetInfo;
  80.  
  81. /*
  82.  * When handling INCR-style selection retrievals, the selection owner
  83.  * uses the following data structure to communicate between the
  84.  * ConvertSelection procedure and TkSelPropProc.
  85.  */
  86.  
  87. typedef struct IncrInfo {
  88.     TkWindow *winPtr;        /* Window that owns selection. */
  89.     Atom *multAtoms;        /* Information about conversions to
  90.                  * perform:  one or more pairs of
  91.                  * (target, property).  This either
  92.                  * points to a retrieved  property (for
  93.                  * MULTIPLE retrievals) or to a static
  94.                  * array. */
  95.     unsigned long numConversions;
  96.                 /* Number of entries in offsets (same as
  97.                  * # of pairs in multAtoms). */
  98.     int *offsets;        /* One entry for each pair in
  99.                  * multAtoms;  -1 means all data has
  100.                  * been transferred for this
  101.                  * conversion.  -2 means only the
  102.                  * final zero-length transfer still
  103.                  * has to be done.  Otherwise it is the
  104.                  * offset of the next chunk of data
  105.                  * to transfer.  This array is malloc-ed. */
  106.     int numIncrs;        /* Number of entries in offsets that
  107.                  * aren't -1 (i.e. # of INCR-mode transfers
  108.                  * not yet completed). */
  109.     Tk_TimerToken timeout;    /* Token for timer procedure. */
  110.     int idleTime;        /* Number of seconds since we heard
  111.                  * anything from the selection
  112.                  * requestor. */
  113.     Window reqWindow;        /* Requestor's window id. */
  114.     Time time;            /* Timestamp corresponding to
  115.                  * selection at beginning of request;
  116.                  * used to abort transfer if selection
  117.                  * changes. */
  118.     struct IncrInfo *nextPtr;    /* Next in list of all INCR-style
  119.                  * retrievals currently pending. */
  120. } IncrInfo;
  121.  
  122. static IncrInfo *pendingIncrs = NULL;
  123.                 /* List of all IncrInfo structures
  124.                  * currently active. */
  125.  
  126. /*
  127.  * When a selection handler is set up by invoking "selection handle",
  128.  * one of the following data structures is set up to hold information
  129.  * about the command to invoke and its interpreter.
  130.  */
  131.  
  132. typedef struct {
  133.     Tcl_Interp *interp;        /* Interpreter in which to invoke command. */
  134.     int cmdLength;        /* # of non-NULL bytes in command. */
  135.     char command[4];        /* Command to invoke.  Actual space is
  136.                  * allocated as large as necessary.  This
  137.                  * must be the last entry in the structure. */
  138. } CommandInfo;
  139.  
  140. /*
  141.  * Chunk size for retrieving selection.  It's defined both in
  142.  * words and in bytes;  the word size is used to allocate
  143.  * buffer space that's guaranteed to be word-aligned and that
  144.  * has an extra character for the terminating NULL.
  145.  */
  146.  
  147. #define TK_SEL_BYTES_AT_ONCE 4000
  148. #define TK_SEL_WORDS_AT_ONCE 1001
  149.  
  150. /*
  151.  * Largest property that we'll accept when sending or receiving the
  152.  * selection:
  153.  */
  154.  
  155. #define MAX_PROP_WORDS 100000
  156.  
  157. /*
  158.  * Forward declarations for procedures defined in this file:
  159.  */
  160.  
  161. static void        ConvertSelection _ANSI_ARGS_((TkWindow *winPtr,
  162.                 XSelectionRequestEvent *eventPtr));
  163. static int        DefaultSelection _ANSI_ARGS_((TkWindow *winPtr,
  164.                 Atom target, char *buffer, int maxBytes,
  165.                 Atom *typePtr));
  166. static int        HandleTclCommand _ANSI_ARGS_((ClientData clientData,
  167.                 int offset, char *buffer, int maxBytes));
  168. static void        IncrTimeoutProc _ANSI_ARGS_((ClientData clientData));
  169. static char *        SelCvtFromX _ANSI_ARGS_((long *propPtr, int numValues,
  170.                 Atom type, Tk_Window tkwin));
  171. static long *        SelCvtToX _ANSI_ARGS_((char *string, Atom type,
  172.                 Tk_Window tkwin, int *numLongsPtr));
  173. static int        SelGetProc _ANSI_ARGS_((ClientData clientData,
  174.                 Tcl_Interp *interp, char *portion));
  175. static void        SelInit _ANSI_ARGS_((Tk_Window tkwin));
  176. static void        SelRcvIncrProc _ANSI_ARGS_((ClientData clientData,
  177.                 XEvent *eventPtr));
  178. static void        SelTimeoutProc _ANSI_ARGS_((ClientData clientData));
  179.  
  180. /*
  181.  *--------------------------------------------------------------
  182.  *
  183.  * Tk_CreateSelHandler --
  184.  *
  185.  *    This procedure is called to register a procedure
  186.  *    as the handler for selection requests of a particular
  187.  *    target type on a particular window.
  188.  *
  189.  * Results:
  190.  *    None.
  191.  *
  192.  * Side effects:
  193.  *    In the future, whenever the selection is in tkwin's
  194.  *    window and someone requests the selection in the
  195.  *    form given by target, proc will be invoked to provide
  196.  *    part or all of the selection in the given form.  If
  197.  *    there was already a handler declared for the given
  198.  *    window and target type, then it is replaced.  Proc
  199.  *    should have the following form:
  200.  *
  201.  *    int
  202.  *    proc(clientData, offset, buffer, maxBytes)
  203.  *        ClientData clientData;
  204.  *        int offset;
  205.  *        char *buffer;
  206.  *        int maxBytes;
  207.  *    {
  208.  *    }
  209.  *
  210.  *    The clientData argument to proc will be the same as
  211.  *    the clientData argument to this procedure.  The offset
  212.  *    argument indicates which portion of the selection to
  213.  *    return:  skip the first offset bytes.  Buffer is a
  214.  *    pointer to an area in which to place the converted
  215.  *    selection, and maxBytes gives the number of bytes
  216.  *    available at buffer.  Proc should place the selection
  217.  *    in buffer as a string, and return a count of the number
  218.  *    of bytes of selection actually placed in buffer (not
  219.  *    including the terminating NULL character).  If the
  220.  *    return value equals maxBytes, this is a sign that there
  221.  *    is probably still more selection information available.
  222.  *
  223.  *--------------------------------------------------------------
  224.  */
  225.  
  226. void
  227. Tk_CreateSelHandler(tkwin, target, proc, clientData, format)
  228.     Tk_Window tkwin;        /* Token for window. */
  229.     Atom target;        /* The kind of selection conversions
  230.                  * that can be handled by proc,
  231.                  * e.g. TARGETS or XA_STRING. */
  232.     Tk_SelectionProc *proc;    /* Procedure to invoke to convert
  233.                  * selection to type "target". */
  234.     ClientData clientData;    /* Value to pass to proc. */
  235.     Atom format;        /* Format in which the selection
  236.                  * information should be returned to
  237.                  * the requestor. XA_STRING is best by
  238.                  * far, but anything listed in the ICCCM
  239.                  * will be tolerated (blech). */
  240. {
  241.     register TkSelHandler *selPtr;
  242.     TkWindow *winPtr = (TkWindow *) tkwin;
  243.  
  244.     if (winPtr->dispPtr->multipleAtom == None) {
  245.     SelInit(tkwin);
  246.     }
  247.  
  248.     /*
  249.      * See if there's already a handler for this target on
  250.      * this window.  If so, re-use it.  If not, create a new one.
  251.      */
  252.  
  253.     for (selPtr = winPtr->selHandlerList; ; selPtr = selPtr->nextPtr) {
  254.     if (selPtr == NULL) {
  255.         selPtr = (TkSelHandler *) ckalloc(sizeof(TkSelHandler));
  256.         selPtr->nextPtr = winPtr->selHandlerList;
  257.         winPtr->selHandlerList = selPtr;
  258.         break;
  259.     }
  260.     if (selPtr->target == target) {
  261.  
  262.         /*
  263.          * Special case:  when replacing handler created by
  264.          * "selection handle" free up memory.  Should there be a
  265.          * callback to allow other clients to do this too?
  266.          */
  267.  
  268.         if (selPtr->proc == HandleTclCommand) {
  269.         ckfree((char *) selPtr->clientData);
  270.         }
  271.         break;
  272.     }
  273.     }
  274.     selPtr->target = target;
  275.     selPtr->format = format;
  276.     selPtr->proc = proc;
  277.     selPtr->clientData = clientData;
  278.     if (format == XA_STRING) {
  279.     selPtr->size = 8;
  280.     } else {
  281.     selPtr->size = 32;
  282.     }
  283. }
  284.  
  285. /*
  286.  *--------------------------------------------------------------
  287.  *
  288.  * Tk_OwnSelection --
  289.  *
  290.  *    Arrange for tkwin to become the selection owner.
  291.  *
  292.  * Results:
  293.  *    None.
  294.  *
  295.  * Side effects:
  296.  *    From now on, requests for the selection will be
  297.  *    directed to procedures associated with tkwin (they
  298.  *    must have been declared with calls to Tk_CreateSelHandler).
  299.  *    When the selection is lost by this window, proc will
  300.  *    be invoked (see the manual entry for details).
  301.  *
  302.  *--------------------------------------------------------------
  303.  */
  304.  
  305. void
  306. Tk_OwnSelection(tkwin, proc, clientData)
  307.     Tk_Window tkwin;        /* Window to become new selection
  308.                  * owner. */
  309.     Tk_LostSelProc *proc;    /* Procedure to call when selection
  310.                  * is taken away from tkwin. */
  311.     ClientData clientData;    /* Arbitrary one-word argument to
  312.                  * pass to proc. */
  313. {
  314.     register TkWindow *winPtr = (TkWindow *) tkwin;
  315.     TkDisplay *dispPtr = winPtr->dispPtr;
  316.  
  317.     if (dispPtr->multipleAtom == None) {
  318.     SelInit(tkwin);
  319.     }
  320.  
  321.     winPtr->selClearProc = proc;
  322.     winPtr->selClearData = clientData;
  323.     if (dispPtr->selectionOwner != tkwin) {
  324.     TkWindow *ownerPtr = (TkWindow *) dispPtr->selectionOwner;
  325.  
  326.     if ((ownerPtr != NULL)
  327.         && (ownerPtr->selClearProc != NULL)) {
  328.         (*ownerPtr->selClearProc)(ownerPtr->selClearData);
  329.         ownerPtr->selClearProc = NULL;
  330.     }
  331.     }
  332.     dispPtr->selectionOwner = tkwin;
  333.     dispPtr->selectionSerial = NextRequest(winPtr->display);
  334.     dispPtr->selectionTime = TkCurrentTime(dispPtr);
  335.     XSetSelectionOwner(winPtr->display, XA_PRIMARY, winPtr->window,
  336.         dispPtr->selectionTime);
  337. }
  338.  
  339. /*
  340.  *--------------------------------------------------------------
  341.  *
  342.  * Tk_GetSelection --
  343.  *
  344.  *    Retrieve the selection and pass it off (in pieces,
  345.  *    possibly) to a given procedure.
  346.  *
  347.  * Results:
  348.  *    The return value is a standard Tcl return value.
  349.  *    If an error occurs (such as no selection exists)
  350.  *    then an error message is left in interp->result.
  351.  *
  352.  * Side effects:
  353.  *    The standard X11 protocols are used to retrieve the
  354.  *    selection.  When it arrives, it is passed to proc.  If
  355.  *    the selection is very large, it will be passed to proc
  356.  *    in several pieces.  Proc should have the following
  357.  *    structure:
  358.  *
  359.  *    int
  360.  *    proc(clientData, interp, portion)
  361.  *        ClientData clientData;
  362.  *        Tcl_Interp *interp;
  363.  *        char *portion;
  364.  *    {
  365.  *    }
  366.  *
  367.  *    The interp and clientData arguments to proc will be the
  368.  *    same as the corresponding arguments to Tk_GetSelection.
  369.  *    The portion argument points to a character string
  370.  *    containing part of the selection, and numBytes indicates
  371.  *    the length of the portion, not including the terminating
  372.  *    NULL character.  If the selection arrives in several pieces,
  373.  *    the "portion" arguments in separate calls will contain
  374.  *    successive parts of the selection.  Proc should normally
  375.  *    return TCL_OK.  If it detects an error then it should return
  376.  *    TCL_ERROR and leave an error message in interp->result; the
  377.  *    remainder of the selection retrieval will be aborted.
  378.  *
  379.  *--------------------------------------------------------------
  380.  */
  381.  
  382. int
  383. Tk_GetSelection(interp, tkwin, target, proc, clientData)
  384.     Tcl_Interp *interp;        /* Interpreter to use for reporting
  385.                  * errors. */
  386.     Tk_Window tkwin;        /* Window on whose behalf to retrieve
  387.                  * the selection (determines display
  388.                  * from which to retrieve). */
  389.     Atom target;        /* Desired form in which selection
  390.                  * is to be returned. */
  391.     Tk_GetSelProc *proc;    /* Procedure to call to process the
  392.                  * selection, once it has been retrieved. */
  393.     ClientData clientData;    /* Arbitrary value to pass to proc. */
  394. {
  395.     RetrievalInfo retr;
  396.     TkWindow *winPtr = (TkWindow *) tkwin;
  397.     TkDisplay *dispPtr = winPtr->dispPtr;
  398.  
  399.     if (dispPtr->multipleAtom == None) {
  400.     SelInit(tkwin);
  401.     }
  402.  
  403.     /*
  404.      * If the selection is owned by a window managed by this
  405.      * process, then call the retrieval procedure directly,
  406.      * rather than going through the X server (it's dangerous
  407.      * to go through the X server in this case because it could
  408.      * result in deadlock if an INCR-style selection results).
  409.      */
  410.  
  411.     if (dispPtr->selectionOwner != NULL) {
  412.     register TkSelHandler *selPtr;
  413.     int offset, result, count;
  414.     char buffer[TK_SEL_BYTES_AT_ONCE+1];
  415.     Time time;
  416.  
  417.     /*
  418.      * Make sure that the selection predates the request
  419.      * time.
  420.      */
  421.  
  422.     time = TkCurrentTime(dispPtr);
  423.     if ((time < dispPtr->selectionTime)
  424.         && (time != CurrentTime)
  425.         && (dispPtr->selectionTime != CurrentTime)) {
  426.         interp->result = "selection changed before it could be retrieved";
  427.         return TCL_ERROR;
  428.     }
  429.  
  430.     for (selPtr = ((TkWindow *) dispPtr->selectionOwner)->selHandlerList;
  431.         ; selPtr = selPtr->nextPtr) {
  432.         if (selPtr == NULL) {
  433.         Atom type;
  434.  
  435.         count = DefaultSelection((TkWindow *) dispPtr->selectionOwner,
  436.             target, buffer, TK_SEL_BYTES_AT_ONCE, &type);
  437.         if (count > TK_SEL_BYTES_AT_ONCE) {
  438.             panic("selection handler returned too many bytes");
  439.         }
  440.         if (count < 0) {
  441.             cantget:
  442.             Tcl_AppendResult(interp, "selection doesn't exist",
  443.                 " or form \"", Tk_GetAtomName(tkwin, target),
  444.                 "\" not defined", (char *) NULL);
  445.             return TCL_ERROR;
  446.         }
  447.         buffer[count] = 0;
  448.         return (*proc)(clientData, interp, buffer);
  449.         }
  450.         if (selPtr->target == target) {
  451.         break;
  452.         }
  453.     }
  454.     offset = 0;
  455.     while (1) {
  456.         count = (*selPtr->proc)(selPtr->clientData, offset,
  457.         buffer, TK_SEL_BYTES_AT_ONCE);
  458.         if (count < 0) {
  459.         goto cantget;
  460.         }
  461.         if (count > TK_SEL_BYTES_AT_ONCE) {
  462.         panic("selection handler returned too many bytes");
  463.         }
  464.         buffer[count] = '\0';
  465.         result = (*proc)(clientData, interp, buffer);
  466.         if (result != TCL_OK) {
  467.         return result;
  468.         }
  469.         if (count < TK_SEL_BYTES_AT_ONCE) {
  470.         return TCL_OK;
  471.         }
  472.         offset += count;
  473.     }
  474.     }
  475.  
  476.     /*
  477.      * The selection is owned by some other process.  To
  478.      * retrieve it, first record information about the retrieval
  479.      * in progress.  Also, try to use a non-top-level window
  480.      * as the requestor (property changes on this window may
  481.      * be monitored by a window manager, which will waste time).
  482.      */
  483.  
  484.     retr.interp = interp;
  485.     if ((winPtr->flags & TK_TOP_LEVEL)
  486.         && (winPtr->childList != NULL)) {
  487.     winPtr = winPtr->childList;
  488.     }
  489.     retr.winPtr = winPtr;
  490.     retr.property = XA_PRIMARY;
  491.     retr.target = target;
  492.     retr.proc = proc;
  493.     retr.clientData = clientData;
  494.     retr.result = -1;
  495.     retr.idleTime = 0;
  496.     retr.nextPtr = pendingRetrievals;
  497.     pendingRetrievals = &retr;
  498.  
  499.     /*
  500.      * Initiate the request for the selection.
  501.      */
  502.  
  503.     XConvertSelection(winPtr->display, XA_PRIMARY, target,
  504.         retr.property, winPtr->window, TkCurrentTime(dispPtr));
  505.  
  506.     /*
  507.      * Enter a loop processing X events until the selection
  508.      * has been retrieved and processed.  If no response is
  509.      * received within a few seconds, then timeout.
  510.      */
  511.  
  512.     retr.timeout = Tk_CreateTimerHandler(1000, SelTimeoutProc,
  513.         (ClientData) &retr);
  514.     while (retr.result == -1) {
  515.     Tk_DoOneEvent(0);
  516.     }
  517.     Tk_DeleteTimerHandler(retr.timeout);
  518.  
  519.     /*
  520.      * Unregister the information about the selection retrieval
  521.      * in progress.
  522.      */
  523.  
  524.     if (pendingRetrievals == &retr) {
  525.     pendingRetrievals = retr.nextPtr;
  526.     } else {
  527.     RetrievalInfo *retrPtr;
  528.  
  529.     for (retrPtr = pendingRetrievals; retrPtr != NULL;
  530.         retrPtr = retrPtr->nextPtr) {
  531.         if (retrPtr->nextPtr == &retr) {
  532.         retrPtr->nextPtr = retr.nextPtr;
  533.         break;
  534.         }
  535.     }
  536.     }
  537.     return retr.result;
  538. }
  539.  
  540. /*
  541.  *--------------------------------------------------------------
  542.  *
  543.  * Tk_SelectionCmd --
  544.  *
  545.  *    This procedure is invoked to process the "selection" Tcl
  546.  *    command.  See the user documentation for details on what
  547.  *    it does.
  548.  *
  549.  * Results:
  550.  *    A standard Tcl result.
  551.  *
  552.  * Side effects:
  553.  *    See the user documentation.
  554.  *
  555.  *--------------------------------------------------------------
  556.  */
  557.  
  558. int
  559. Tk_SelectionCmd(clientData, interp, argc, argv)
  560.     ClientData clientData;    /* Main window associated with
  561.                  * interpreter. */
  562.     Tcl_Interp *interp;        /* Current interpreter. */
  563.     int argc;            /* Number of arguments. */
  564.     char **argv;        /* Argument strings. */
  565. {
  566.     Tk_Window tkwin = (Tk_Window) clientData;
  567.     int length;
  568.     char c;
  569.  
  570.     if (argc < 2) {
  571.     sprintf(interp->result,
  572.         "wrong # args: should be \"%.50s option ?arg arg ...?\"",
  573.         argv[0]);
  574.     return TCL_ERROR;
  575.     }
  576.     c = argv[1][0];
  577.     length = strlen(argv[1]);
  578.     if ((c == 'g') && (strncmp(argv[1], "get", length) == 0)) {
  579.     Atom target;
  580.     GetInfo getInfo;
  581.     int result;
  582.  
  583.     if (argc > 3) {
  584.         sprintf(interp->result,
  585.             "too may args: should be \"%.50s get ?type?\"",
  586.             argv[0]);
  587.         return TCL_ERROR;
  588.     }
  589.     if (argc == 3) {
  590.         target = Tk_InternAtom(tkwin, argv[2]);
  591.     } else {
  592.         target = XA_STRING;
  593.     }
  594.     getInfo.string = (char *) ckalloc(100);
  595.     getInfo.bytesAvl = 100;
  596.     getInfo.bytesUsed = 0;
  597.     result = Tk_GetSelection(interp, tkwin, target, SelGetProc,
  598.         (ClientData) &getInfo);
  599.     if (result == TCL_OK) {
  600.         Tcl_SetResult(interp, getInfo.string, TCL_DYNAMIC);
  601.     } else {
  602.         ckfree(getInfo.string);
  603.     }
  604.     return result;
  605.     } else if ((c == 'h') && (strncmp(argv[1], "handle", length) == 0)) {
  606.     Tk_Window window;
  607.     Atom target, format;
  608.     register CommandInfo *cmdInfoPtr;
  609.     int cmdLength;
  610.  
  611.     if ((argc < 4) || (argc > 6)) {
  612.         Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  613.             " handle window command ?type? ?format?\"", (char *) NULL);
  614.         return TCL_ERROR;
  615.     }
  616.     window = Tk_NameToWindow(interp, argv[2], tkwin);
  617.     if (window == NULL) {
  618.         return TCL_ERROR;
  619.     }
  620.     if (argc > 4) {
  621.         target = Tk_InternAtom(window, argv[4]);
  622.     } else {
  623.         target = XA_STRING;
  624.     }
  625.     if (argc > 5) {
  626.         format = Tk_InternAtom(window, argv[5]);
  627.     } else {
  628.         format = XA_STRING;
  629.     }
  630.     cmdLength = strlen(argv[3]);
  631.     cmdInfoPtr = (CommandInfo *) ckalloc((unsigned) (sizeof(CommandInfo)
  632.         + cmdLength));
  633.     cmdInfoPtr->interp = interp;
  634.     cmdInfoPtr->cmdLength = cmdLength;
  635.     strcpy(cmdInfoPtr->command, argv[3]);
  636.     Tk_CreateSelHandler(window, target, HandleTclCommand,
  637.         (ClientData) cmdInfoPtr, format);
  638.     return TCL_OK;
  639.     } else {
  640.     sprintf(interp->result,
  641.         "bad option \"%.50s\":  must be get or handle",
  642.         argv[1]);
  643.     return TCL_ERROR;
  644.     }
  645. }
  646.  
  647. /*
  648.  *----------------------------------------------------------------------
  649.  *
  650.  * TkSelDeadWindow --
  651.  *
  652.  *    This procedure is invoked just before a TkWindow is deleted.
  653.  *    It performs selection-related cleanup.
  654.  *
  655.  * Results:
  656.  *    None.
  657.  *
  658.  * Side effects:
  659.  *    Frees up memory associated with the selection.
  660.  *
  661.  *----------------------------------------------------------------------
  662.  */
  663.  
  664. void
  665. TkSelDeadWindow(winPtr)
  666.     register TkWindow *winPtr;    /* Window that's being deleted. */
  667. {
  668.     register TkSelHandler *selPtr;
  669.  
  670.     while (1) {
  671.     selPtr = winPtr->selHandlerList;
  672.     if (selPtr == NULL) {
  673.         break;
  674.     }
  675.     winPtr->selHandlerList = selPtr->nextPtr;
  676.     ckfree((char *) selPtr);
  677.     }
  678.     winPtr->selClearProc = NULL;
  679.  
  680.     if (winPtr->dispPtr->selectionOwner == (Tk_Window) winPtr) {
  681.     winPtr->dispPtr->selectionOwner = NULL;
  682.     }
  683. }
  684.  
  685. /*
  686.  *----------------------------------------------------------------------
  687.  *
  688.  * SelInit --
  689.  *
  690.  *    Initialize selection-related information for a display.
  691.  *
  692.  * Results:
  693.  *    None.
  694.  *
  695.  * Side effects:
  696.  *    .
  697.  *
  698.  *----------------------------------------------------------------------
  699.  */
  700.  
  701. static void
  702. SelInit(tkwin)
  703.     Tk_Window tkwin;        /* Window token (used to find
  704.                  * display to initialize). */
  705. {
  706.     register TkDisplay *dispPtr = ((TkWindow *) tkwin)->dispPtr;
  707.  
  708.     /*
  709.      * Fetch commonly-used atoms.
  710.      */
  711.  
  712.     dispPtr->multipleAtom = Tk_InternAtom(tkwin, "MULTIPLE");
  713.     dispPtr->incrAtom = Tk_InternAtom(tkwin, "INCR");
  714.     dispPtr->targetsAtom = Tk_InternAtom(tkwin, "TARGETS");
  715.     dispPtr->timestampAtom = Tk_InternAtom(tkwin, "TIMESTAMP");
  716.     dispPtr->textAtom = Tk_InternAtom(tkwin, "TEXT");
  717.     dispPtr->compoundTextAtom = Tk_InternAtom(tkwin, "COMPOUND_TEXT");
  718. }
  719.  
  720. /*
  721.  *--------------------------------------------------------------
  722.  *
  723.  * TkSelEventProc --
  724.  *
  725.  *    This procedure is invoked whenever a selection-related
  726.  *    event occurs.  It does the lion's share of the work
  727.  *    in implementing the selection protocol.
  728.  *
  729.  * Results:
  730.  *    None.
  731.  *
  732.  * Side effects:
  733.  *    Lots:  depends on the type of event.
  734.  *
  735.  *--------------------------------------------------------------
  736.  */
  737.  
  738. void
  739. TkSelEventProc(tkwin, eventPtr)
  740.     Tk_Window tkwin;        /* Window for which event was
  741.                  * targeted. */
  742.     register XEvent *eventPtr;    /* X event:  either SelectionClear,
  743.                  * SelectionRequest, or
  744.                  * SelectionNotify. */
  745. {
  746.     register TkWindow *winPtr = (TkWindow *) tkwin;
  747.  
  748.     /*
  749.      * Case #1: SelectionClear events.  Invoke clear procedure
  750.      * for window that just lost the selection.  This code is a
  751.      * bit tricky, because any callbacks to due selection changes
  752.      * between windows managed by the process have already been
  753.      * made.  Thus, ignore the event unless it refers to the
  754.      * window that's currently the selection owner and the event
  755.      * was generated after the server saw the SetSelectionOwner
  756.      * request.
  757.      */
  758.  
  759.     if (eventPtr->type == SelectionClear) {
  760.     if ((eventPtr->xselectionclear.selection == XA_PRIMARY)
  761.         && (winPtr->dispPtr->selectionOwner == tkwin)
  762.         && (eventPtr->xselectionclear.serial
  763.             >= winPtr->dispPtr->selectionSerial)
  764.         && (winPtr->selClearProc != NULL)) {
  765.         (*winPtr->selClearProc)(winPtr->selClearData);
  766.         winPtr->selClearProc = NULL;
  767.         winPtr->dispPtr->selectionOwner = NULL;
  768.     }
  769.     return;
  770.     }
  771.  
  772.     /*
  773.      * Case #2: SelectionNotify events.  Call the relevant procedure
  774.      * to handle the incoming selection.
  775.      */
  776.  
  777.     if (eventPtr->type == SelectionNotify) {
  778.     register RetrievalInfo *retrPtr;
  779.     char *propInfo;
  780.     Atom type;
  781.     int format, result;
  782.     unsigned long numItems, bytesAfter;
  783.  
  784.     for (retrPtr = pendingRetrievals; ; retrPtr = retrPtr->nextPtr) {
  785.         if (retrPtr == NULL) {
  786.         return;
  787.         }
  788.         if ((retrPtr->winPtr == winPtr)
  789.             && (eventPtr->xselection.selection == XA_PRIMARY)
  790.             && (retrPtr->target == eventPtr->xselection.target)
  791.             && (retrPtr->result == -1)) {
  792.         if (retrPtr->property == eventPtr->xselection.property) {
  793.             break;
  794.         }
  795.         if (eventPtr->xselection.property == None) {
  796.             Tcl_SetResult(retrPtr->interp, (char *) NULL, TCL_STATIC);
  797.             Tcl_AppendResult(retrPtr->interp,
  798.                 "selection doesn't exist or form \"",
  799.                 Tk_GetAtomName(tkwin, retrPtr->target),
  800.                 "\" not defined", (char *) NULL);
  801.             retrPtr->result = TCL_ERROR;
  802.             return;
  803.         }
  804.         }
  805.     }
  806.  
  807.     propInfo = NULL;
  808.     result = XGetWindowProperty(eventPtr->xselection.display,
  809.         eventPtr->xselection.requestor, retrPtr->property,
  810.         0, MAX_PROP_WORDS, False, (Atom) AnyPropertyType,
  811.         &type, &format, &numItems, &bytesAfter,
  812.         (unsigned char **) &propInfo);
  813.     if ((result != Success) || (type == None)) {
  814.         return;
  815.     }
  816.     if (bytesAfter != 0) {
  817.         Tcl_SetResult(retrPtr->interp, "selection property too large",
  818.         TCL_STATIC);
  819.         retrPtr->result = TCL_ERROR;
  820.         XFree(propInfo);
  821.         return;
  822.     }
  823.     if ((type == XA_STRING) || (type == winPtr->dispPtr->textAtom)
  824.         || (type == winPtr->dispPtr->compoundTextAtom)) {
  825.         if (format != 8) {
  826.         sprintf(retrPtr->interp->result,
  827.             "bad format for string selection: wanted \"8\", got \"%d\"",
  828.             format);
  829.         retrPtr->result = TCL_ERROR;
  830.         return;
  831.         }
  832.         retrPtr->result = (*retrPtr->proc)(retrPtr->clientData,
  833.             retrPtr->interp, propInfo);
  834.     } else if (type == winPtr->dispPtr->incrAtom) {
  835.  
  836.         /*
  837.          * It's a !?#@!?!! INCR-style reception.  Arrange to receive
  838.          * the selection in pieces, using the ICCCM protocol, then
  839.          * hang around until either the selection is all here or a
  840.          * timeout occurs.
  841.          */
  842.  
  843.         retrPtr->idleTime = 0;
  844.         Tk_CreateEventHandler(tkwin, PropertyChangeMask, SelRcvIncrProc,
  845.             (ClientData) retrPtr);
  846.         XDeleteProperty(Tk_Display(tkwin), Tk_WindowId(tkwin),
  847.             retrPtr->property);
  848.         while (retrPtr->result == -1) {
  849.         Tk_DoOneEvent(0);
  850.         }
  851.         Tk_DeleteEventHandler(tkwin, PropertyChangeMask, SelRcvIncrProc,
  852.             (ClientData) retrPtr);
  853.     } else {
  854.         char *string;
  855.  
  856.         if (format != 32) {
  857.         sprintf(retrPtr->interp->result,
  858.             "bad format for selection: wanted \"32\", got \"%d\"",
  859.             format);
  860.         retrPtr->result = TCL_ERROR;
  861.         return;
  862.         }
  863.         string = SelCvtFromX((long *) propInfo, (int) numItems, type,
  864.             (Tk_Window) winPtr);
  865.         retrPtr->result = (*retrPtr->proc)(retrPtr->clientData,
  866.             retrPtr->interp, string);
  867.         ckfree(string);
  868.     }
  869.     XFree(propInfo);
  870.     return;
  871.     }
  872.  
  873.     /*
  874.      * Case #3: SelectionRequest events.  Call ConvertSelection to
  875.      * do the dirty work.
  876.      */
  877.  
  878.     if ((eventPtr->type == SelectionRequest)
  879.         && (eventPtr->xselectionrequest.selection == XA_PRIMARY)) {
  880.     ConvertSelection(winPtr, &eventPtr->xselectionrequest);
  881.     return;
  882.     }
  883. }
  884.  
  885. /*
  886.  *--------------------------------------------------------------
  887.  *
  888.  * SelGetProc --
  889.  *
  890.  *    This procedure is invoked to process pieces of the
  891.  *    selection as they arrive during "selection get"
  892.  *    commands.
  893.  *
  894.  * Results:
  895.  *    Always returns TCL_OK.
  896.  *
  897.  * Side effects:
  898.  *    Bytes get appended to the result currently stored
  899.  *    in interp->result, and its memory area gets
  900.  *    expanded if necessary.
  901.  *
  902.  *--------------------------------------------------------------
  903.  */
  904.  
  905.     /* ARGSUSED */
  906. static int
  907. SelGetProc(clientData, interp, portion)
  908.     ClientData clientData;    /* Information about partially-
  909.                  * assembled result. */
  910.     Tcl_Interp *interp;        /* Interpreter used for error
  911.                  * reporting (not used). */
  912.     char *portion;        /* New information to be appended. */
  913. {
  914.     register GetInfo *getInfoPtr = (GetInfo *) clientData;
  915.     int newLength;
  916.  
  917.     newLength = strlen(portion) + getInfoPtr->bytesUsed;
  918.  
  919.     /*
  920.      * Grow the result area if we've run out of space.
  921.      */
  922.  
  923.     if (newLength >= getInfoPtr->bytesAvl) {
  924.     char *newString;
  925.  
  926.     getInfoPtr->bytesAvl *= 2;
  927.     if (getInfoPtr->bytesAvl <= newLength) {
  928.         getInfoPtr->bytesAvl = newLength + 1;
  929.     }
  930.     newString = (char *) ckalloc((unsigned) getInfoPtr->bytesAvl);
  931.     memcpy((VOID *) newString, (VOID *) getInfoPtr->string,
  932.         getInfoPtr->bytesUsed);
  933.     ckfree(getInfoPtr->string);
  934.     getInfoPtr->string = newString;
  935.     }
  936.  
  937.     /*
  938.      * Append the new data to what was already there.
  939.      */
  940.  
  941.     strcpy(getInfoPtr->string + getInfoPtr->bytesUsed, portion);
  942.     getInfoPtr->bytesUsed = newLength;
  943.     return TCL_OK;
  944. }
  945.  
  946. /*
  947.  *----------------------------------------------------------------------
  948.  *
  949.  * SelCvtToX --
  950.  *
  951.  *    Given a selection represented as a string (the normal Tcl form),
  952.  *    convert it to the ICCCM-mandated format for X, depending on
  953.  *    the type argument.  This procedure and SelCvtFromX are inverses.
  954.  *
  955.  * Results:
  956.  *    The return value is a malloc'ed buffer holding a value
  957.  *    equivalent to "string", but formatted as for "type".  It is
  958.  *    the caller's responsibility to free the string when done with
  959.  *    it.  The word at *numLongsPtr is filled in with the number of
  960.  *    32-bit words returned in the result.
  961.  *
  962.  * Side effects:
  963.  *    None.
  964.  *
  965.  *----------------------------------------------------------------------
  966.  */
  967.  
  968. static long *
  969. SelCvtToX(string, type, tkwin, numLongsPtr)
  970.     char *string;        /* String representation of selection. */
  971.     Atom type;            /* Atom specifying the X format that is
  972.                  * desired for the selection.  Should not
  973.                  * be XA_STRING (if so, don't bother calling
  974.                  * this procedure at all). */
  975.     Tk_Window tkwin;        /* Window that governs atom conversion. */
  976.     int *numLongsPtr;        /* Number of 32-bit words contained in the
  977.                  * result. */
  978. {
  979.     register char *p;
  980.     char *field;
  981.     int numFields;
  982.     long *propPtr, *longPtr;
  983. #define MAX_ATOM_NAME_LENGTH 100
  984.     char atomName[MAX_ATOM_NAME_LENGTH+1];
  985.  
  986.     /*
  987.      * The string is assumed to consist of fields separated by spaces.
  988.      * The property gets generated by converting each field to an
  989.      * integer number, in one of two ways:
  990.      * 1. If type is XA_ATOM, convert each field to its corresponding
  991.      *      atom.
  992.      * 2. If type is anything else, convert each field from an ASCII number
  993.      *    to a 32-bit binary number.
  994.      */
  995.  
  996.     numFields = 1;
  997.     for (p = string; *p != 0; p++) {
  998.     if (isspace(*p)) {
  999.         numFields++;
  1000.     }
  1001.     }
  1002.     propPtr = (long *) ckalloc((unsigned) numFields*sizeof(long));
  1003.  
  1004.     /*
  1005.      * Convert the fields one-by-one.
  1006.      */
  1007.  
  1008.     for (longPtr = propPtr, *numLongsPtr = 0, p = string;
  1009.         ; longPtr++, (*numLongsPtr)++) {
  1010.     while (isspace(*p)) {
  1011.         p++;
  1012.     }
  1013.     if (*p == 0) {
  1014.         break;
  1015.     }
  1016.     field = p;
  1017.     while ((*p != 0) && !isspace(*p)) {
  1018.         p++;
  1019.     }
  1020.     if (type == XA_ATOM) {
  1021.         int length;
  1022.  
  1023.         length = p - field;
  1024.         if (length > MAX_ATOM_NAME_LENGTH) {
  1025.         length = MAX_ATOM_NAME_LENGTH;
  1026.         }
  1027.         strncpy(atomName, field, length);
  1028.         atomName[length] = 0;
  1029.         *longPtr = (long) Tk_InternAtom(tkwin, atomName);
  1030.     } else {
  1031.         char *dummy;
  1032.  
  1033.         *longPtr = strtol(field, &dummy, 0);
  1034.     }
  1035.     }
  1036.     return propPtr;
  1037. }
  1038.  
  1039. /*
  1040.  *----------------------------------------------------------------------
  1041.  *
  1042.  * SelCvtFromX --
  1043.  *
  1044.  *    Given an X property value, formatted as a collection of 32-bit
  1045.  *    values according to "type" and the ICCCM conventions, convert
  1046.  *    the value to a string suitable for manipulation by Tcl.  This
  1047.  *    procedure is the inverse of SelCvtToX.
  1048.  *
  1049.  * Results:
  1050.  *    The return value is the string equivalent of "property".  It is
  1051.  *    malloc-ed and should be freed by the caller when no longer
  1052.  *    needed.
  1053.  *
  1054.  * Side effects:
  1055.  *    None.
  1056.  *
  1057.  *----------------------------------------------------------------------
  1058.  */
  1059.  
  1060. static char *
  1061. SelCvtFromX(propPtr, numValues, type, tkwin)
  1062.     register long *propPtr;    /* Property value from X. */
  1063.     int numValues;        /* Number of 32-bit values in property. */
  1064.     Atom type;            /* Type of property  Should not be
  1065.                  * XA_STRING (if so, don't bother calling
  1066.                  * this procedure at all). */
  1067.     Tk_Window tkwin;        /* Window to use for atom conversion. */
  1068. {
  1069.     char *result;
  1070.     int resultSpace, curSize, fieldSize;
  1071.     char *atomName;
  1072.  
  1073.     /*
  1074.      * Convert each long in the property to a string value, which is
  1075.      * either the name of an atom (if type is XA_ATOM) or a hexadecimal
  1076.      * string.  Make an initial guess about the size of the result, but
  1077.      * be prepared to enlarge the result if necessary.
  1078.      */
  1079.  
  1080.     resultSpace = 12*numValues;
  1081.     curSize = 0;
  1082.     atomName = "";    /* Not needed, but eliminates compiler warning. */
  1083.     result = (char *) ckalloc((unsigned) resultSpace);
  1084.     for ( ; numValues > 0; propPtr++, numValues--) {
  1085.     if (type == XA_ATOM) {
  1086.         atomName = Tk_GetAtomName(tkwin, (Atom) *propPtr);
  1087.         fieldSize = strlen(atomName) + 1;
  1088.     } else {
  1089.         fieldSize = 12;
  1090.     }
  1091.     if (curSize+fieldSize >= resultSpace) {
  1092.         char *newResult;
  1093.  
  1094.         resultSpace *= 2;
  1095.         if (curSize+fieldSize >= resultSpace) {
  1096.         resultSpace = curSize + fieldSize + 1;
  1097.         }
  1098.         newResult = (char *) ckalloc((unsigned) resultSpace);
  1099.         strcpy(newResult, result);
  1100.         ckfree(result);
  1101.         result = newResult;
  1102.     }
  1103.     if (curSize != 0) {
  1104.         result[curSize] = ' ';
  1105.         curSize++;
  1106.     }
  1107.     if (type == XA_ATOM) {
  1108.         strcpy(result+curSize, atomName);
  1109.     } else {
  1110.         sprintf(result+curSize, "%#x", *propPtr);
  1111.     }
  1112.     curSize += strlen(result+curSize);
  1113.     }
  1114.     return result;
  1115. }
  1116.  
  1117. /*
  1118.  *----------------------------------------------------------------------
  1119.  *
  1120.  * ConvertSelection --
  1121.  *
  1122.  *    This procedure is invoked to handle SelectionRequest events.
  1123.  *    It responds to the requests, obeying the ICCCM protocols.
  1124.  *
  1125.  * Results:
  1126.  *    None.
  1127.  *
  1128.  * Side effects:
  1129.  *    Properties are created for the selection requestor, and a
  1130.  *    SelectionNotify event is generated for the selection
  1131.  *    requestor.  In the event of long selections, this procedure
  1132.  *    implements INCR-mode transfers, using the ICCCM protocol.
  1133.  *
  1134.  *----------------------------------------------------------------------
  1135.  */
  1136.  
  1137. static void
  1138. ConvertSelection(winPtr, eventPtr)
  1139.     TkWindow *winPtr;            /* Window that owns selection. */
  1140.     register XSelectionRequestEvent *eventPtr;
  1141.                     /* Event describing request. */
  1142. {
  1143.     XSelectionEvent reply;        /* Used to notify requestor that
  1144.                      * selection info is ready. */
  1145.     int multiple;            /* Non-zero means a MULTIPLE request
  1146.                      * is being handled. */
  1147.     IncrInfo info;            /* State of selection conversion. */
  1148.     Atom singleInfo[2];            /* info.multAtoms points here except
  1149.                      * for multiple conversions. */
  1150.     int i;
  1151.     Tk_ErrorHandler errorHandler;
  1152.  
  1153.     errorHandler = Tk_CreateErrorHandler(eventPtr->display, -1, -1,-1,
  1154.         (int (*)()) NULL, (ClientData) NULL);
  1155.  
  1156.     /*
  1157.      * Initialize the reply event.
  1158.      */
  1159.  
  1160.     reply.type = SelectionNotify;
  1161.     reply.serial = 0;
  1162.     reply.send_event = True;
  1163.     reply.display = eventPtr->display;
  1164.     reply.requestor = eventPtr->requestor;
  1165.     reply.selection = XA_PRIMARY;
  1166.     reply.target = eventPtr->target;
  1167.     reply.property = eventPtr->property;
  1168.     if (reply.property == None) {
  1169.     reply.property = reply.target;
  1170.     }
  1171.     reply.time = eventPtr->time;
  1172.  
  1173.     /*
  1174.      * Watch out for races between conversion requests and
  1175.      * selection ownership changes:  reject the conversion
  1176.      * request if it's for the wrong window or the wrong
  1177.      * time.
  1178.      */
  1179.  
  1180.     if ((winPtr->dispPtr->selectionOwner != (Tk_Window) winPtr)
  1181.         || ((eventPtr->time < winPtr->dispPtr->selectionTime)
  1182.         && (eventPtr->time != CurrentTime)
  1183.         && (winPtr->dispPtr->selectionTime != CurrentTime))) {
  1184.     goto refuse;
  1185.     }
  1186.  
  1187.     /*
  1188.      * Figure out which kind(s) of conversion to perform.  If handling
  1189.      * a MULTIPLE conversion, then read the property describing which
  1190.      * conversions to perform.
  1191.      */
  1192.  
  1193.     info.winPtr = winPtr;
  1194.     if (eventPtr->target != winPtr->dispPtr->multipleAtom) {
  1195.     multiple = 0;
  1196.     singleInfo[0] = reply.target;
  1197.     singleInfo[1] = reply.property;
  1198.     info.multAtoms = singleInfo;
  1199.     info.numConversions = 1;
  1200.     } else {
  1201.     Atom type;
  1202.     int format, result;
  1203.     unsigned long bytesAfter;
  1204.  
  1205.     multiple = 1;
  1206.     info.multAtoms = NULL;
  1207.     if (eventPtr->property == None) {
  1208.         goto refuse;
  1209.     }
  1210.     result = XGetWindowProperty(eventPtr->display,
  1211.         eventPtr->requestor, eventPtr->property,
  1212.         0, MAX_PROP_WORDS, False, XA_ATOM,
  1213.         &type, &format, &info.numConversions, &bytesAfter,
  1214.         (unsigned char **) &info.multAtoms);
  1215.     if ((result != Success) || (bytesAfter != 0) || (format != 32)
  1216.         || (type == None)) {
  1217.         if (info.multAtoms != NULL) {
  1218.         XFree((char *) info.multAtoms);
  1219.         }
  1220.         goto refuse;
  1221.     }
  1222.     info.numConversions /= 2;        /* Two atoms per conversion. */
  1223.     }
  1224.  
  1225.     /*
  1226.      * Loop through all of the requested conversions, and either return
  1227.      * the entire converted selection, if it can be returned in a single
  1228.      * bunch, or return INCR information only (the actual selection will
  1229.      * be returned below).
  1230.      */
  1231.  
  1232.     info.offsets = (int *) ckalloc((unsigned) (info.numConversions*sizeof(int)));
  1233.     info.numIncrs = 0;
  1234.     for (i = 0; i < info.numConversions; i++) {
  1235.     Atom target, property;
  1236.     long buffer[TK_SEL_WORDS_AT_ONCE];
  1237.     register TkSelHandler *selPtr;
  1238.  
  1239.     target = info.multAtoms[2*i];
  1240.     property = info.multAtoms[2*i + 1];
  1241.     info.offsets[i] = -1;
  1242.  
  1243.     for (selPtr = winPtr->selHandlerList; ; selPtr = selPtr->nextPtr) {
  1244.         int numItems, format;
  1245.         char *propPtr;
  1246.         Atom type;
  1247.  
  1248.         if (selPtr == NULL) {
  1249.  
  1250.         /*
  1251.          * Nobody seems to know about this kind of request.  If
  1252.          * it's of a sort that we can handle without any help, do
  1253.          * it.  Otherwise mark the request as an errror.
  1254.          */
  1255.  
  1256.         numItems = DefaultSelection(winPtr, target, (char *) buffer,
  1257.             TK_SEL_BYTES_AT_ONCE, &type);
  1258.         if (numItems != 0) {
  1259.             goto gotStuff;
  1260.         }
  1261.         info.multAtoms[2*i + 1] = None;
  1262.         break;
  1263.         } else if (selPtr->target == target) {
  1264.         numItems = (*selPtr->proc)(selPtr->clientData, 0,
  1265.             (char *) buffer, TK_SEL_BYTES_AT_ONCE);
  1266.         if (numItems < 0) {
  1267.             info.multAtoms[2*i + 1] = None;
  1268.             break;
  1269.         }
  1270.         if (numItems > TK_SEL_BYTES_AT_ONCE) {
  1271.             panic("selection handler returned too many bytes");
  1272.         }
  1273.         ((char *) buffer)[numItems] = '\0';
  1274.         type = selPtr->format;
  1275.         } else {
  1276.         continue;
  1277.         }
  1278.  
  1279.         gotStuff:
  1280.         if (numItems == TK_SEL_BYTES_AT_ONCE) {
  1281.         info.numIncrs++;
  1282.         type = winPtr->dispPtr->incrAtom;
  1283.         buffer[0] = 10;    /* Guess at # items avl. */
  1284.         numItems = 1;
  1285.         propPtr = (char *) buffer;
  1286.         format = 32;
  1287.         info.offsets[i] = 0;
  1288.         } else if (type == XA_STRING) {
  1289.         propPtr = (char *) buffer;
  1290.         format = 8;
  1291.         } else {
  1292.         propPtr = (char *) SelCvtToX((char *) buffer,
  1293.             type, (Tk_Window) winPtr, &numItems);
  1294.         format = 32;
  1295.         }
  1296.         XChangeProperty(reply.display, reply.requestor,
  1297.             property, type, format, PropModeReplace,
  1298.             (unsigned char *) propPtr, numItems);
  1299.         if (propPtr != (char *) buffer) {
  1300.         ckfree(propPtr);
  1301.         }
  1302.         break;
  1303.     }
  1304.     }
  1305.  
  1306.     /*
  1307.      * Send an event back to the requestor to indicate that the
  1308.      * first stage of conversion is complete (everything is done
  1309.      * except for long conversions that have to be done in INCR
  1310.      * mode).
  1311.      */
  1312.  
  1313.     if (info.numIncrs > 0) {
  1314.     XSelectInput(reply.display, reply.requestor, PropertyChangeMask);
  1315.     info.timeout = Tk_CreateTimerHandler(1000, IncrTimeoutProc,
  1316.         (ClientData) &info);
  1317.     info.idleTime = 0;
  1318.     info.reqWindow = reply.requestor;
  1319.     info.time = winPtr->dispPtr->selectionTime;
  1320.     info.nextPtr = pendingIncrs;
  1321.     pendingIncrs = &info;
  1322.     }
  1323.     if (multiple) {
  1324.     XChangeProperty(reply.display, reply.requestor, reply.property,
  1325.         XA_ATOM, 32, PropModeReplace,
  1326.         (unsigned char *) info.multAtoms,
  1327.         (int) info.numConversions*2);
  1328.     } else {
  1329.  
  1330.     /*
  1331.      * Not a MULTIPLE request.  The first property in "multAtoms"
  1332.      * got set to None if there was an error in conversion.
  1333.      */
  1334.  
  1335.     reply.property = info.multAtoms[1];
  1336.     }
  1337.     XSendEvent(reply.display, reply.requestor, False, 0, (XEvent *) &reply);
  1338.     Tk_DeleteErrorHandler(errorHandler);
  1339.  
  1340.     /*
  1341.      * Handle any remaining INCR-mode transfers.  This all happens
  1342.      * in callbacks to TkSelPropProc, so just wait until the number
  1343.      * of uncompleted INCR transfers drops to zero.
  1344.      */
  1345.  
  1346.     if (info.numIncrs > 0) {
  1347.     IncrInfo *infoPtr2;
  1348.  
  1349.     while (info.numIncrs > 0) {
  1350.         Tk_DoOneEvent(0);
  1351.     }
  1352.     Tk_DeleteTimerHandler(info.timeout);
  1353.     errorHandler = Tk_CreateErrorHandler(winPtr->display,
  1354.         -1, -1,-1, (int (*)()) NULL, (ClientData) NULL);
  1355.     XSelectInput(reply.display, reply.requestor, 0L);
  1356.     Tk_DeleteErrorHandler(errorHandler);
  1357.     if (pendingIncrs == &info) {
  1358.         pendingIncrs = info.nextPtr;
  1359.     } else {
  1360.         for (infoPtr2 = pendingIncrs; infoPtr2 != NULL;
  1361.             infoPtr2 = infoPtr2->nextPtr) {
  1362.         if (infoPtr2->nextPtr == &info) {
  1363.             infoPtr2->nextPtr = info.nextPtr;
  1364.             break;
  1365.         }
  1366.         }
  1367.     }
  1368.     }
  1369.  
  1370.     /*
  1371.      * All done.  Cleanup and return.
  1372.      */
  1373.  
  1374.     ckfree((char *) info.offsets);
  1375.     if (multiple) {
  1376.     XFree((char *) info.multAtoms);
  1377.     }
  1378.     return;
  1379.  
  1380.     /*
  1381.      * An error occurred.  Send back a refusal message.
  1382.      */
  1383.  
  1384.     refuse:
  1385.     reply.property = None;
  1386.     XSendEvent(reply.display, reply.requestor, False, 0, (XEvent *) &reply);
  1387.     Tk_DeleteErrorHandler(errorHandler);
  1388.     return;
  1389. }
  1390.  
  1391. /*
  1392.  *----------------------------------------------------------------------
  1393.  *
  1394.  * SelRcvIncrProc --
  1395.  *
  1396.  *    This procedure handles the INCR protocol on the receiving
  1397.  *    side.  It is invoked in response to property changes on
  1398.  *    the requestor's window (which hopefully are because a new
  1399.  *    chunk of the selection arrived).
  1400.  *
  1401.  * Results:
  1402.  *    None.
  1403.  *
  1404.  * Side effects:
  1405.  *    If a new piece of selection has arrived, a procedure is
  1406.  *    invoked to deal with that piece.  When the whole selection
  1407.  *    is here, a flag is left for the higher-level procedure that
  1408.  *    initiated the selection retrieval.
  1409.  *
  1410.  *----------------------------------------------------------------------
  1411.  */
  1412.  
  1413. static void
  1414. SelRcvIncrProc(clientData, eventPtr)
  1415.     ClientData clientData;        /* Information about retrieval. */
  1416.     register XEvent *eventPtr;        /* X PropertyChange event. */
  1417. {
  1418.     register RetrievalInfo *retrPtr = (RetrievalInfo *) clientData;
  1419.     char *propInfo;
  1420.     Atom type;
  1421.     int format, result;
  1422.     unsigned long numItems, bytesAfter;
  1423.  
  1424.     if ((eventPtr->xproperty.atom != retrPtr->property)
  1425.         || (eventPtr->xproperty.state != PropertyNewValue)
  1426.         || (retrPtr->result != -1)) {
  1427.     return;
  1428.     }
  1429.     propInfo = NULL;
  1430.     result = XGetWindowProperty(eventPtr->xproperty.display,
  1431.         eventPtr->xproperty.window, retrPtr->property, 0, MAX_PROP_WORDS,
  1432.         True, (Atom) AnyPropertyType, &type, &format, &numItems,
  1433.         &bytesAfter, (unsigned char **) &propInfo);
  1434.     if ((result != Success) || (type == None)) {
  1435.     return;
  1436.     }
  1437.     if (bytesAfter != 0) {
  1438.     Tcl_SetResult(retrPtr->interp, "selection property too large",
  1439.         TCL_STATIC);
  1440.     retrPtr->result = TCL_ERROR;
  1441.     goto done;
  1442.     }
  1443.     if (numItems == 0) {
  1444.     retrPtr->result = TCL_OK;
  1445.     } else if ((type == XA_STRING)
  1446.         || (type == retrPtr->winPtr->dispPtr->textAtom)
  1447.         || (type == retrPtr->winPtr->dispPtr->compoundTextAtom)) {
  1448.     if (format != 8) {
  1449.         Tcl_SetResult(retrPtr->interp, (char *) NULL, TCL_STATIC);
  1450.         sprintf(retrPtr->interp->result,
  1451.         "bad format for string selection: wanted \"8\", got \"%d\"",
  1452.         format);
  1453.         retrPtr->result = TCL_ERROR;
  1454.         goto done;
  1455.     }
  1456.     result = (*retrPtr->proc)(retrPtr->clientData, retrPtr->interp,
  1457.         propInfo);
  1458.     if (result != TCL_OK) {
  1459.         retrPtr->result = result;
  1460.     }
  1461.     } else {
  1462.     char *string;
  1463.  
  1464.     if (format != 32) {
  1465.         Tcl_SetResult(retrPtr->interp, (char *) NULL, TCL_STATIC);
  1466.         sprintf(retrPtr->interp->result,
  1467.         "bad format for selection: wanted \"32\", got \"%d\"",
  1468.         format);
  1469.         retrPtr->result = TCL_ERROR;
  1470.         goto done;
  1471.     }
  1472.     string = SelCvtFromX((long *) propInfo, (int) numItems, type,
  1473.         (Tk_Window) retrPtr->winPtr);
  1474.     result = (*retrPtr->proc)(retrPtr->clientData, retrPtr->interp,
  1475.         string);
  1476.     if (result != TCL_OK) {
  1477.         retrPtr->result = result;
  1478.     }
  1479.     ckfree(string);
  1480.     }
  1481.  
  1482.     done:
  1483.     XFree(propInfo);
  1484.     retrPtr->idleTime = 0;
  1485. }
  1486.  
  1487. /*
  1488.  *----------------------------------------------------------------------
  1489.  *
  1490.  * TkSelPropProc --
  1491.  *
  1492.  *    This procedure is invoked when property-change events
  1493.  *    occur on windows not known to the toolkit.  Its function
  1494.  *    is to implement the sending side of the INCR selection
  1495.  *    retrieval protocol when the selection requestor deletes
  1496.  *    the property containing a part of the selection.
  1497.  *
  1498.  * Results:
  1499.  *    None.
  1500.  *
  1501.  * Side effects:
  1502.  *    If the property that is receiving the selection was just
  1503.  *    deleted, then a new piece of the selection is fetched and
  1504.  *    placed in the property, until eventually there's no more
  1505.  *    selection to fetch.
  1506.  *
  1507.  *----------------------------------------------------------------------
  1508.  */
  1509.  
  1510. void
  1511. TkSelPropProc(eventPtr)
  1512.     register XEvent *eventPtr;        /* X PropertyChange event. */
  1513. {
  1514.     register IncrInfo *infoPtr;
  1515.     int i, format;
  1516.     Atom target;
  1517.     register TkSelHandler *selPtr;
  1518.     long buffer[TK_SEL_WORDS_AT_ONCE];
  1519.     int numItems;
  1520.     char *propPtr;
  1521.     Tk_ErrorHandler errorHandler;
  1522.  
  1523.     /*
  1524.      * See if this event announces the deletion of a property being
  1525.      * used for an INCR transfer.  If so, then add the next chunk of
  1526.      * data to the property.
  1527.      */
  1528.  
  1529.     if (eventPtr->xproperty.state != PropertyDelete) {
  1530.     return;
  1531.     }
  1532.     for (infoPtr = pendingIncrs; infoPtr != NULL;
  1533.         infoPtr = infoPtr->nextPtr) {
  1534.  
  1535.     /*
  1536.      * To avoid races between selection conversions and
  1537.      * changes in selection ownership, make sure the window
  1538.      * and timestamp for the current selection match those
  1539.      * in the INCR request.
  1540.      */
  1541.  
  1542.     if ((infoPtr->reqWindow != eventPtr->xproperty.window)
  1543.         || (infoPtr->winPtr->dispPtr->selectionOwner
  1544.             != (Tk_Window) infoPtr->winPtr)
  1545.         || (infoPtr->winPtr->dispPtr->selectionTime
  1546.         != infoPtr->time)) {
  1547.         continue;
  1548.     }
  1549.     for (i = 0; i < infoPtr->numConversions; i++) {
  1550.         if ((eventPtr->xproperty.atom != infoPtr->multAtoms[2*i + 1])
  1551.             || (infoPtr->offsets[i] == -1)){
  1552.         continue;
  1553.         }
  1554.         target = infoPtr->multAtoms[2*i];
  1555.         infoPtr->idleTime = 0;
  1556.         for (selPtr = infoPtr->winPtr->selHandlerList; ;
  1557.             selPtr = selPtr->nextPtr) {
  1558.         if (selPtr == NULL) {
  1559.             infoPtr->multAtoms[2*i + 1] = None;
  1560.             infoPtr->offsets[i] = -1;
  1561.             infoPtr->numIncrs --;
  1562.             return;
  1563.         }
  1564.         if (selPtr->target == target) {
  1565.             if (infoPtr->offsets[i] == -2) {
  1566.             numItems = 0;
  1567.             ((char *) buffer)[0] = 0;
  1568.             } else {
  1569.             numItems = (*selPtr->proc)(selPtr->clientData,
  1570.                 infoPtr->offsets[i], (char *) buffer,
  1571.                 TK_SEL_BYTES_AT_ONCE);
  1572.             if (numItems > TK_SEL_BYTES_AT_ONCE) {
  1573.                 panic("selection handler returned too many bytes");
  1574.             } else {
  1575.                 if (numItems < 0) {
  1576.                 numItems = 0;
  1577.                 }
  1578.             }
  1579.             ((char *) buffer)[numItems] = '\0';
  1580.             }
  1581.             if (numItems < TK_SEL_BYTES_AT_ONCE) {
  1582.             if (numItems <= 0) {
  1583.                 infoPtr->offsets[i] = -1;
  1584.                 infoPtr->numIncrs--;
  1585.             } else {
  1586.                 infoPtr->offsets[i] = -2;
  1587.             }
  1588.             } else {
  1589.             infoPtr->offsets[i] += numItems;
  1590.             }
  1591.             if (selPtr->format == XA_STRING) {
  1592.             propPtr = (char *) buffer;
  1593.             format = 8;
  1594.             } else {
  1595.             propPtr = (char *) SelCvtToX((char *) buffer,
  1596.                 selPtr->format,
  1597.                 (Tk_Window) infoPtr->winPtr,
  1598.                 &numItems);
  1599.             format = 32;
  1600.             }
  1601.             errorHandler = Tk_CreateErrorHandler(
  1602.                 eventPtr->xproperty.display, -1, -1, -1,
  1603.                 (int (*)()) NULL, (ClientData) NULL);
  1604.             XChangeProperty(eventPtr->xproperty.display,
  1605.                 eventPtr->xproperty.window,
  1606.                 eventPtr->xproperty.atom, selPtr->format,
  1607.                 format, PropModeReplace,
  1608.                 (unsigned char *) propPtr, numItems);
  1609.             Tk_DeleteErrorHandler(errorHandler);
  1610.             if (propPtr != (char *) buffer) {
  1611.             ckfree(propPtr);
  1612.             }
  1613.             return;
  1614.         }
  1615.         }
  1616.     }
  1617.     }
  1618. }
  1619.  
  1620. /*
  1621.  *----------------------------------------------------------------------
  1622.  *
  1623.  * HandleTclCommand --
  1624.  *
  1625.  *    This procedure acts as selection handler for handlers created
  1626.  *    by the "selection handle" command.  It invokes a Tcl command to
  1627.  *    retrieve the selection.
  1628.  *
  1629.  * Results:
  1630.  *    The return value is a count of the number of bytes actually
  1631.  *    stored at buffer.
  1632.  *
  1633.  * Side effects:
  1634.  *    None except for things done by the Tcl command.
  1635.  *
  1636.  *----------------------------------------------------------------------
  1637.  */
  1638.  
  1639. static int
  1640. HandleTclCommand(clientData, offset, buffer, maxBytes)
  1641.     ClientData clientData;    /* Information about command to execute. */
  1642.     int offset;            /* Return selection bytes starting at this
  1643.                  * offset. */
  1644.     char *buffer;        /* Place to store converted selection. */
  1645.     int maxBytes;        /* Maximum # of bytes to store at buffer. */
  1646. {
  1647.     register CommandInfo *cmdInfoPtr = (CommandInfo *) clientData;
  1648.     char *oldResultString;
  1649.     Tcl_FreeProc *oldFreeProc;
  1650.     int spaceNeeded, length;
  1651. #define MAX_STATIC_SIZE 100
  1652.     char staticSpace[MAX_STATIC_SIZE];
  1653.     char *command;
  1654.  
  1655.     /*
  1656.      * First, generate a command by taking the command string
  1657.      * and appending the offset and maximum # of bytes.
  1658.      */
  1659.  
  1660.     spaceNeeded = cmdInfoPtr->cmdLength + 30;
  1661.     if (spaceNeeded < MAX_STATIC_SIZE) {
  1662.     command = staticSpace;
  1663.     } else {
  1664.     command = (char *) ckalloc((unsigned) spaceNeeded);
  1665.     }
  1666.     sprintf(command, "%s %d %d", cmdInfoPtr->command, offset, maxBytes);
  1667.  
  1668.     /*
  1669.      * Execute the command.  Be sure to restore the state of the
  1670.      * interpreter after executing the command.
  1671.      */
  1672.  
  1673.     oldFreeProc = cmdInfoPtr->interp->freeProc;
  1674.     if (oldFreeProc != 0) {
  1675.     oldResultString = cmdInfoPtr->interp->result;
  1676.     } else {
  1677.     oldResultString = (char *) ckalloc((unsigned)
  1678.         (strlen(cmdInfoPtr->interp->result) + 1));
  1679.     strcpy(oldResultString, cmdInfoPtr->interp->result);
  1680.     oldFreeProc = TCL_DYNAMIC;
  1681.     }
  1682.     cmdInfoPtr->interp->freeProc = 0;
  1683.     if (Tcl_GlobalEval(cmdInfoPtr->interp, command) == TCL_OK) {
  1684.     length = strlen(cmdInfoPtr->interp->result);
  1685.     } else {
  1686.     length = 0;
  1687.     }
  1688.     if (length > maxBytes) {
  1689.     length = maxBytes;
  1690.     }
  1691.     memcpy((VOID *) buffer, (VOID *) cmdInfoPtr->interp->result, length);
  1692.     buffer[length] = '\0';
  1693.     Tcl_FreeResult(cmdInfoPtr->interp);
  1694.     cmdInfoPtr->interp->result = oldResultString;
  1695.     cmdInfoPtr->interp->freeProc = oldFreeProc;
  1696.  
  1697.     if (command != staticSpace) {
  1698.     ckfree(command);
  1699.     }
  1700.  
  1701.     return length;
  1702. }
  1703.  
  1704. /*
  1705.  *----------------------------------------------------------------------
  1706.  *
  1707.  * SelTimeoutProc --
  1708.  *
  1709.  *    This procedure is invoked once every second while waiting for
  1710.  *    the selection to be returned.  After a while it gives up and
  1711.  *    aborts the selection retrieval.
  1712.  *
  1713.  * Results:
  1714.  *    None.
  1715.  *
  1716.  * Side effects:
  1717.  *    A new timer callback is created to call us again in another
  1718.  *    second, unless time has expired, in which case an error is
  1719.  *    recorded for the retrieval.
  1720.  *
  1721.  *----------------------------------------------------------------------
  1722.  */
  1723.  
  1724. static void
  1725. SelTimeoutProc(clientData)
  1726.     ClientData clientData;        /* Information about retrieval
  1727.                      * in progress. */
  1728. {
  1729.     register RetrievalInfo *retrPtr = (RetrievalInfo *) clientData;
  1730.  
  1731.     /*
  1732.      * Make sure that the retrieval is still in progress.  Then
  1733.      * see how long it's been since any sort of response was received
  1734.      * from the other side.
  1735.      */
  1736.  
  1737.     if (retrPtr->result != -1) {
  1738.     return;
  1739.     }
  1740.     retrPtr->idleTime++;
  1741.     if (retrPtr->idleTime >= 5) {
  1742.  
  1743.     /*
  1744.      * Use a careful procedure to store the error message, because
  1745.      * the result could already be partially filled in with a partial
  1746.      * selection return.
  1747.      */
  1748.  
  1749.     Tcl_SetResult(retrPtr->interp, "selection owner didn't respond",
  1750.         TCL_STATIC);
  1751.     retrPtr->result = TCL_ERROR;
  1752.     } else {
  1753.     retrPtr->timeout = Tk_CreateTimerHandler(1000, SelTimeoutProc,
  1754.         (ClientData) retrPtr);
  1755.     }
  1756. }
  1757.  
  1758. /*
  1759.  *----------------------------------------------------------------------
  1760.  *
  1761.  * IncrTimeoutProc --
  1762.  *
  1763.  *    This procedure is invoked once a second while sending the
  1764.  *    selection to a requestor in INCR mode.  After a while it
  1765.  *    gives up and aborts the selection operation.
  1766.  *
  1767.  * Results:
  1768.  *    None.
  1769.  *
  1770.  * Side effects:
  1771.  *    A new timeout gets registered so that this procedure gets
  1772.  *    called again in another second, unless too many seconds
  1773.  *    have elapsed, in which case infoPtr is marked as "all done".
  1774.  *
  1775.  *----------------------------------------------------------------------
  1776.  */
  1777.  
  1778. static void
  1779. IncrTimeoutProc(clientData)
  1780.     ClientData clientData;        /* Information about INCR-mode
  1781.                      * selection retrieval for which
  1782.                      * we are selection owner. */
  1783. {
  1784.     register IncrInfo *infoPtr = (IncrInfo *) clientData;
  1785.  
  1786.     infoPtr->idleTime++;
  1787.     if (infoPtr->idleTime >= 5) {
  1788.     infoPtr->numIncrs = 0;
  1789.     } else {
  1790.     infoPtr->timeout = Tk_CreateTimerHandler(1000, IncrTimeoutProc,
  1791.         (ClientData) infoPtr);
  1792.     }
  1793. }
  1794.  
  1795. /*
  1796.  *----------------------------------------------------------------------
  1797.  *
  1798.  * DefaultSelection --
  1799.  *
  1800.  *    This procedure is called to generate selection information
  1801.  *    for a few standard targets such as TIMESTAMP and TARGETS.
  1802.  *    It is invoked only if no handler has been declared by the
  1803.  *    application.
  1804.  *
  1805.  * Results:
  1806.  *    If "target" is a standard target understood by this procedure,
  1807.  *    the selection is converted to that form and stored as a
  1808.  *    character string in buffer.  The type of the selection (e.g.
  1809.  *    STRING or ATOM) is stored in *typePtr, and the return value is
  1810.  *    a count of the # of non-NULL bytes at buffer.  If the target
  1811.  *    wasn't understood, or if there isn't enough space at buffer
  1812.  *    to hold the entire selection (no INCR-mode transfers for this
  1813.  *    stuff!), then -1 is returned.
  1814.  *
  1815.  * Side effects:
  1816.  *    None.
  1817.  *
  1818.  *----------------------------------------------------------------------
  1819.  */
  1820.  
  1821. static int
  1822. DefaultSelection(winPtr, target, buffer, maxBytes, typePtr)
  1823.     TkWindow *winPtr;        /* Window that owns selection. */
  1824.     Atom target;        /* Desired form of selection. */
  1825.     char *buffer;        /* Place to put selection characters. */
  1826.     int maxBytes;        /* Maximum # of bytes to store at buffer. */
  1827.     Atom *typePtr;        /* Store here the type of the selection,
  1828.                  * for use in converting to proper X format. */
  1829. {
  1830.     if (target == winPtr->dispPtr->timestampAtom) {
  1831.     if (maxBytes < 20) {
  1832.         return -1;
  1833.     }
  1834.     sprintf(buffer, "%#x", winPtr->dispPtr->selectionTime);
  1835.     *typePtr = XA_INTEGER;
  1836.     return strlen(buffer);
  1837.     }
  1838.  
  1839.     if (target == winPtr->dispPtr->targetsAtom) {
  1840.     register TkSelHandler *selPtr;
  1841.     char *atomString;
  1842.     int length, atomLength;
  1843.  
  1844.     if (maxBytes < 50) {
  1845.         return -1;
  1846.     }
  1847.     strcpy(buffer, "TARGETS MULTIPLE TIMESTAMP");
  1848.     length = strlen(buffer);
  1849.     for (selPtr = winPtr->selHandlerList; selPtr != NULL;
  1850.         selPtr = selPtr->nextPtr) {
  1851.         atomString = Tk_GetAtomName((Tk_Window) winPtr, selPtr->target);
  1852.         atomLength = strlen(atomString) + 1;
  1853.         if ((length + atomLength) >= maxBytes) {
  1854.         return -1;
  1855.         }
  1856.         sprintf(buffer+length, " %s", atomString);
  1857.         length += atomLength;
  1858.     }
  1859.     *typePtr = XA_ATOM;
  1860.     return length;
  1861.     }
  1862.  
  1863.     return -1;
  1864. }
  1865.